package net.knightwatch.kwtools.walkthrough.items;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class RadioButtonAnswer extends Answer{
	public static final String TAG = "RadioButtonAnswer";
	private ArrayList<String> answerOptions;
	private String answer;
	private RadioGroup radioGroup;
	
	public RadioButtonAnswer(){
		this.setAnswerTypeId(RADIOBUTTON_TYPE);
		this.answerOptions = new ArrayList<String>();
	}
	
	public ArrayList<String> getAnswerOptions() {
		return answerOptions;
	}

	public void setAnswerOptions(ArrayList<String> answerOptions) {
		this.answerOptions = answerOptions;
	}

	@Override
	public LinearLayout getAnswerLayout(Context context) {
		// Create the layout that will contain the answer items
		HorizontalScrollView sv = new HorizontalScrollView(context);
		LinearLayout ll = new LinearLayout(context);
		radioGroup = new RadioGroup(context);
		radioGroup.setOrientation(RadioGroup.HORIZONTAL);
		for (String answerOption : this.answerOptions){
			Log.d(TAG, "Adding option = " + answerOption);
			RadioButton option = new RadioButton(context); 
			option.setText(answerOption);
			option.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {
					if(isChecked){
					setAnswer("" + buttonView.getText());
					Log.d(TAG, "answer = " + getAnswer());
					}
				}});
			radioGroup.addView(option);
		}
		sv.addView(radioGroup);
		ll.addView(sv);
		return ll;
	}

	@Override
	public String toString() {
		return answer;
	}

	public String getAnswer() {
		return answer;
	}

	public void setAnswer(String answer) {
		this.answer = answer;
	}

	public String getOptionId() {
		return null;
	}

	@Override
	public void updateAnswerView() {
		// TODO remove?
		
	}

	@Override
	public void setAnswerText(String answerText) {
		// TODO Update the Radio Button Answer with text provided
		for(int i = 0; i < radioGroup.getChildCount(); i++){
			RadioButton rb = (RadioButton)radioGroup.getChildAt(i);
			Log.d(TAG, "Test: "+rb.getText()+ " = "+answerText);
			if(rb.getText().equals(answerText)){
				rb.setChecked(true);
			}
		}	
	}

}
